package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Checkin;
import com.pojo.SysUser;
import com.service.CheckinService;
import com.service.UserService;
import com.util.ConstUtils;
import com.vo.CheckinVo;
import com.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/12/4 - 15:24
 */
@RestController
@RequestMapping("/checkin")
public class CheckinController {
    @Autowired
    private CheckinService checkinService;
    @Autowired
    private UserService userService;

    /**
     * 查询入住信息
     *
     * @param checkinVo
     * @return
     */
    @RequestMapping("/list")
    public DataVO<Checkin> findCheckinList(CheckinVo checkinVo) {
        PageHelper.startPage(checkinVo.getPage(), checkinVo.getLimit());
        List<Checkin> checkinList = checkinService.findCheckinList(checkinVo);
        PageInfo<Checkin> pageInfo = new PageInfo<Checkin>(checkinList);
        return new DataVO<Checkin>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加入住的信息
     *
     * @param checkin
     * @return maps
     */
    @RequestMapping("/add")
    public Map<String, Object> addCheckin(Checkin checkin, Principal principal) {
        SysUser loginUser = userService.findUserInfoByUserName(principal.getName());
        checkin.setCreatedBy(loginUser.getId());
        Map<String, Object> maps = new HashMap<String, Object>();
        if (checkinService.addCheckin(checkin) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "登记成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "登记失败，请重试！");
        }
        return maps;
    }

    /**
     * 用户退房处理
     *
     * @param checkinId
     * @return
     */
    @RequestMapping("/checkout")
    public Map<String, Object> checkout(Integer checkinId, Principal principal) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (checkinService.deleteCheckin(checkinId, principal)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "退房成功，请携带您的物品到前台办理退房手续！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "退房失败，请重试！");
        }
        return maps;
    }
}
